package com.rainfull.ui;

import java.util.List;

import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.rainfull.core.IRUIAPNotify;
import com.rainfull.core.RAPItemInfo;
import com.rainfull.core.RAPMgr;
import com.rainfull.ui.RUIApEnable.RUIApEnableTask;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.FrameLayout.LayoutParams;


public class RUISetting extends Activity  implements OnClickListener, IRUIAPNotify
{
	private main m_mainActive = null;
	private CheckBox m_btnSetting = null;
	private RAPMgr m_APMgr = null;
	private int m_nAPState = -1;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
        m_mainActive = (main)this.getParent();
        LinearLayout ll = (LinearLayout) findViewById(R.id.setting_ll_1);
        ll.setLayoutParams(new LayoutParams(m_mainActive.getScreenWidth(), 
        		LayoutParams.FILL_PARENT));
        m_btnSetting = (CheckBox) findViewById(R.id.setting_checkbox_open);
        m_btnSetting.setOnClickListener((android.view.View.OnClickListener) this); 
        m_APMgr = new RAPMgr(this, this);
        m_nAPState = m_APMgr.getWifiState();  
        if (m_nAPState == WifiManager.WIFI_STATE_ENABLED ||
        		m_nAPState == WifiManager.WIFI_STATE_ENABLING)
        {
        	m_btnSetting.setEnabled(true);
        }
        else
        {
        	m_btnSetting.setEnabled(false);
        }
        new RUITestTask().execute(null);
        	
    }
    public boolean onKeyDown(int keyCode, KeyEvent event) 
	{
		return false;
	}
    public void onClick(View v) 
    {
		if (m_btnSetting.isChecked() == false)
		{
			m_btnSetting.setEnabled(false);
			new RUIApEnableTask().execute(null);			
		}
    }

	public void onApConnStateChange(NetworkInfo networkInfo, String strBSSID) {
		// TODO Auto-generated method stub
		
	}

	public void onScanResult(List<ScanResult> aplist) {
		// TODO Auto-generated method stub
		
	}

	public void onWifiStateChanged(int nState) 
	{
    	switch(nState)
    	{
	    	case WifiManager.WIFI_STATE_ENABLED:    					    
	    	case WifiManager.WIFI_STATE_ENABLING:
			    {        	
			    	m_btnSetting.setChecked(true);
			    	m_btnSetting.setEnabled(true);
			    }
			    break;
	    	case WifiManager.WIFI_STATE_DISABLING:			    
	    	case WifiManager.WIFI_STATE_DISABLED:
			    {
			    	m_btnSetting.setChecked(false);
			    	m_btnSetting.setEnabled(false);
			    }
			    break;
    	}		
	}
	 //登陆注册接口
	public class RUIApEnableTask extends AsyncTask
	{
		@Override
		protected Object doInBackground(Object... param) 
		{
			m_APMgr.setWifiState(false);
			return null;
		}
	}
	public class RUITestTask extends AsyncTask
	{
		@Override
		protected Object doInBackground(Object... param) 
		{
			Intent intent = new Intent(RUISetting.this, RUIGMap.class);
			startActivityForResult(intent, 2);
			return null;
		}
		protected void onPostExecute(Object result) 
		{  
			
		}
	}
}
